<?php
require_once(dirname(__FILE__).'/../jssdk.php');
require_once(dirname(__FILE__).'/../wx/WxConfig.php');

$appId = WxConfig::APPID;
$appSecret = WxConfig::APPSECRET;
$jssdk = new JSSDK($appId, $appSecret);
$g_AccessToken=$jssdk->getAccessToken();

function httpGet($url) {
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    // 为保证第三方服务器与微信服务器之间数据传输的安全性，所有微信接口采用https方式调用，必须使用下面2行代码打开ssl安全校验。
    // 如果在部署过程中代码在此处验证失败，请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2 );
	//curl_setopt($curl,CURLOPT_CAINFO,dirname(__FILE__).'/cacert.pem');//这是根据http://curl.haxx.se/ca/cacert.pem 下载的证书，添加这句话之后就运行正常了
	curl_setopt($curl, CURLOPT_URL, $url);
	$res = curl_exec($curl);
	curl_close($curl);
	return $res;
}


function getManage($mid){
	global $dosql;
	if($mid>0){
		$m=$dosql->GetOne("SELECT * FROM `#@__user` WHERE id='$mid' ORDER BY id desc");
		if(is_array($m) && isset($m['id']) ){
			return  $m['truename'];
		}
	}

}
function getRepairMode($mid){
	global $dosql;
	if($mid>0){
		$m=$dosql->GetOne("SELECT p.`classname`,t.`title` FROM `#@__task` as t left join `#@__tasktype` as p  on t.classid=p.id  WHERE t.id='$mid' ");
		if(is_array($m) && isset($m['title']) ){
			return $m['classname']."-".$m['title'];
		}
	}

}


function getTypeNameFromTask($mid){
    global $dosql;
    if($mid>0){
        $m=$dosql->GetOne("SELECT p.`classname`,t.`title` FROM `#@__task` as t left join `#@__tasktype` as p  on t.classid=p.id  WHERE t.id='$mid' ");
        if(is_array($m) && isset($m['title']) ){
            return $m['classname'];
        }
    }

}


function getTypeName($mid){
    global $dosql;
    if($mid>0){
        $m=$dosql->GetOne("SELECT `classname` from `#@__tasktype`  WHERE id='$mid' ");
        if(is_array($m) && isset($m['classname']) ){
            return $m['classname'];
        }
    }

}


function getTaskName($mid){
    global $dosql;
    if($mid>0){
        $m=$dosql->GetOne("SELECT t.`title` FROM `#@__task` as t  WHERE t.id='$mid' ");
        if(is_array($m) && isset($m['title']) ){
            return $m['title'];
        }
    }

}


function getDeviceName($mid){
	global $dosql;
	if($mid>0){
		$m=$dosql->GetOne("SELECT * FROM `#@__cascadedata` WHERE   datagroup='device' AND  datavalue='$mid'  ORDER BY id desc");
		if(is_array($m) && isset($m['id']) ){
			return $m['dataname'];
		}
	}

}
//获取 级联数据
function cascadedata($datagroup,$mid){
	global $dosql;
	if($mid>0){
		$m=$dosql->GetOne("SELECT * FROM `#@__cascadedata` WHERE   datagroup='$datagroup' AND  datavalue='$mid'  ORDER BY id desc");
		if(is_array($m) && isset($m['id']) ){
			return $m['dataname'];
		}
	}

}
#lon为经度，lat为纬度，一定不要弄错了哦
function distance($lon1, $lat1, $lon2, $lat2){
	$d= (2*ATAN2(SQRT(SIN(($lat1-$lat2)*PI()/180/2)
		*SIN(($lat1-$lat2)*PI()/180/2)+
		COS($lat2*PI()/180)*COS($lat1*PI()/180)
		*SIN(($lon1-$lon2)*PI()/180/2)
		*SIN(($lon1-$lon2)*PI()/180/2)),
	SQRT(1-SIN(($lat1-$lat2)*PI()/180/2)
		*SIN(($lat1-$lat2)*PI()/180/2)
		+COS($lat2*PI()/180)*COS($lat1*PI()/180)
		*SIN(($lon1-$lon2)*PI()/180/2)
		*SIN(($lon1-$lon2)*PI()/180/2))))*6378140;
	$d_arr=explode(".",$d);
	return $d_arr[0];
}
// 计算导航距离
#lon为经度，lat为纬度，一定不要弄错了哦
function driving_distance($lng1, $lat1, $lng2, $lat2){
	$distance="";
	$url='https://apis.map.qq.com/ws/direction/v1/driving/?from='.$lat1.','.$lng1.'&to='.$lat2.','.$lng2.'&policy=&output=json&callback=cb&key=DRXBZ-Q3CHG-6KOQN-IJY6G-LDCS5-W4BZZ';
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HEADER, 1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
	//curl_setopt($ch, CURLOPT_POST, 1);
	//curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
	$html = curl_exec($ch);
	$json_arr=json_decode($html,true);
	if($json_arr['status']==0){
		$distance=$json_arr['result']['routes'][0]['distance'];
	}
	return $distance;
}
//获取 离职人员的用户信息
function getNoWoking($mid){
	global $dosql;
	if($mid!=''){
		$m=$dosql->GetOne("SELECT * FROM `user` WHERE uid='$mid' ");
		if(is_array($m) ){
			return $m;
		}
	}

}
//获取员工上个月 派单补贴
function getManegeMonthFee($manageid){
	global $dosql;
	$timestamp=time();
	$firstday=date('Y',$timestamp).'-'.(date('m',$timestamp)-1).'-01';
	$m_start=strtotime($firstday);
	$m_end=strtotime($firstday . '+1 month');
	$sql="SELECT * FROM `#@__fee_log` WHERE manageid='$manageid'  AND op_time > '$m_start' AND op_time < '$m_end' ";
	$dosql->Execute($sql,7);
	$all_fee=0;
	while($order=$dosql->GetArray(7)){
		if($order['op']=='add'){
			$all_fee+=$order['fee'];
		}elseif($order['op']=='move'){
			$all_fee-=$order['fee'];
		}
	}
	return $all_fee;
}

/*
 * 函数说明：获取 工作人员 的名称
 *
 * @access  public
 * @param   $id   int  碎片ID
 * @param   $t    int  调用的内容 0为内容 1为标识名称 2为缩略图 3为跳转连接
 * @return  string     返回碎片缩略图地址
 */
function GetManageName($id = 0)
{
    global $dosql;
    if ($id > 0) {
        $r = $dosql->GetOne("SELECT `truename`  FROM `#@__user` WHERE `id`=$id");
        if (isset($r) && is_array($r)) {
            return $r['truename'];
        } else {
            return "";
        }
    } else {
        return "";
    }
}

?>
